import 'dart:io';
import 'package:flutter/material.dart';
import 'package:extended_image/extended_image.dart';
import 'package:photo_manager/photo_manager.dart';

class ImagePreviewPage extends StatefulWidget {
  final List<AssetEntity> images; // 新增图片列表参数[1,3](@ref)
  final int initialIndex; // 新增初始索引参数[6](@ref)

  const ImagePreviewPage({
    super.key,
    required this.images,
    required this.initialIndex,
  });

  @override
  State<ImagePreviewPage> createState() => _ImagePreviewPageState();
}

class _ImagePreviewPageState extends State<ImagePreviewPage> {
  late PageController _pageController;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _currentIndex = widget.initialIndex;
    _pageController = PageController(initialPage: widget.initialIndex);
    _pageController.addListener(_pageListener);
  }

  void _pageListener() {
    final newPage = _pageController.page?.round() ?? 0;
    if (newPage != _currentIndex) {
      setState(() => _currentIndex = newPage);
    }
  }

  @override
  void dispose() {
    _pageController.removeListener(_pageListener);
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${_currentIndex + 1}/${widget.images.length}'),
        actions: [
          IconButton(
            icon: const Icon(Icons.close),
            onPressed: () => Navigator.pop(context),
          ),
        ],
      ),
      body: Stack(
        children: [
          // 主预览区域
          _buildPageView(),
          
          // 底部指示器[1,3](@ref)
          Positioned(
            bottom: 30,
            left: 0,
            right: 0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(widget.images.length, (index) => 
                Container(
                  width: 8,
                  height: 8,
                  margin: const EdgeInsets.symmetric(horizontal: 4),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: _currentIndex == index 
                      ? Colors.white 
                      : Colors.white.withOpacity(0.5),
                  ),
                )
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildPageView() {
    return PageView.builder(
      controller: _pageController,
      itemCount: widget.images.length,
      onPageChanged: (index) => setState(() => _currentIndex = index),
      itemBuilder: (context, index) {
        return FutureBuilder<File?>(
          future: widget.images[index].file,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ExtendedImage.file(
                snapshot.data!,
                mode: ExtendedImageMode.gesture,
                fit: BoxFit.contain,
                initGestureConfigHandler: (state) => GestureConfig(
                  minScale: 0.8,
                  maxScale: 5.0,
                  animationMinScale: 0.8,
                  animationMaxScale: 5.0,
                  speed: 1.0,
                  inertialSpeed: 100.0,
                  initialScale: 1.0,
                  inPageView: true, // 关键参数：启用页面视图模式[7](@ref)
                ),
              );
            }
            return const Center(child: CircularProgressIndicator());
          },
        );
      },
    );
  }
}